"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : SQL_DDL_Drop_Owned
Case Name   : old_role和new_role权限验证
Create At   : 2022/08/25
Owner       : @li-xin12345
Description :
    1.创建用户
    2.使用用户1和用户3分别创建表
    3.old_role普通用户，new_role普通用户
    4.old_role普通用户，new_role管理员用户
    5.old_role管理员用户户，new_role普通用户
    6.old_role管理员用户，new_role管理员用户
    7.删除表，删除用户
Expect      :
    1.创建成功
    2.创建成功
    3.修改成功
    4.修改成功
    5.修改成功
    6.修改成功
    7.删除成功
History     :
    Modified by @kyeleze 2022/11/17:优化用例，保证\d查询结果匹配
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import macro


class DropOwned(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.user_name_1 = 'u_reassign_owned_0004_1'
        self.user_name_2 = 'u_reassign_owned_0004_2'
        self.user_name_3 = 'u_reassign_owned_0004_3'
        self.user_name_4 = 'u_reassign_owned_0004_4'
        self.table_name_1 = 't_reassign_owned_0004_1'
        self.table_name_2 = 't_reassign_owned_0004_2'

    def test_drop_owned(self):
        text = "-----step1:创建用户;expect:创建成功-----"
        self.logger.info(text)
        create_user = f"drop user if exists {self.user_name_1}," \
            f"{self.user_name_2},{self.user_name_3},{self.user_name_4};" \
            f"create user {self.user_name_1} " \
            f"with password '{macro.COMMON_PASSWD}';" \
            f"grant create any table to {self.user_name_1};" \
            f"create user {self.user_name_2} " \
            f"with password '{macro.COMMON_PASSWD}';" \
            f"create user {self.user_name_3} " \
            f"sysadmin password '{macro.COMMON_PASSWD}';" \
            f"create user {self.user_name_4} " \
            f"sysadmin password '{macro.COMMON_PASSWD}';"
        create_msg = self.primary_sh.execut_db_sql(create_user)
        self.logger.info(create_msg)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.GRANT_SUCCESS_MSG,
                      create_msg, '执行失败' + text)
        self.assertEquals(4, create_msg.count(
            self.constant.CREATE_ROLE_SUCCESS_MSG), '执行失败' + text)

        text = "-----step2:使用用户1和用户3分别创建表;expect:创建成功-----"
        self.logger.info(text)
        sql_cmd = f"drop table if exists {self.table_name_1};" \
            f"create table public.{self.table_name_1}" \
            f"(id int, name varchar(15));" \
            f"insert into {self.table_name_1} values(1, '软件开发');"
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_name_1} -W \'{macro.COMMON_PASSWD}\'')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql(
            '\\d', f'-U {self.user_name_1} -W \'{macro.COMMON_PASSWD}\'')
        self.logger.info(sql_msg)
        self.assertGreater(len(sql_msg.splitlines()), 2, '执行失败' + text)
        self.assertIn(self.table_name_1, sql_msg.splitlines()[-2],
                      '执行失败' + text)
        self.assertIn(self.user_name_1, sql_msg.splitlines()[-2],
                      '执行失败' + text)
        sql_cmd = f"drop table if exists {self.table_name_2};" \
            f"create table public.{self.table_name_2}" \
            f"(id int, name varchar(15));" \
            f"insert into {self.table_name_2} " \
            f"values(1, '软件开发'),(2, '软件测试');"
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_name_3} -W \'{macro.COMMON_PASSWD}\'')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql(
            '\\d', f'-U {self.user_name_3} -W \'{macro.COMMON_PASSWD}\'')
        self.logger.info(sql_msg)
        self.assertGreater(len(sql_msg.splitlines()), 2, '执行失败' + text)
        self.assertIn(self.table_name_2, sql_msg.splitlines()[-2],
                      '执行失败' + text)
        self.assertIn(self.user_name_3, sql_msg.splitlines()[-2],
                      '执行失败' + text)

        text = "-----step3:old_role普通用户，new_role普通用户;expect:修改成功-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_1} " \
            f"to {self.user_name_2};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.reassign_owned_success,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql('\\d')
        self.logger.info(sql_msg)
        self.assertGreater(len(sql_msg.splitlines()), 3, '执行失败' + text)
        self.assertIn(self.table_name_1, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.user_name_2, sql_msg.splitlines()[-3],
                      '执行失败' + text)

        text = "-----step4:old_role普通用户，new_role管理员用户;expect:修改成功-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_2} " \
            f"to {self.user_name_4};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.reassign_owned_success,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql('\\d')
        self.logger.info(sql_msg)
        self.assertGreater(len(sql_msg.splitlines()), 3, '执行失败' + text)
        self.assertIn(self.table_name_1, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.user_name_4, sql_msg.splitlines()[-3],
                      '执行失败' + text)

        text = "-----step5:old_role管理员用户，new_role普通用户;expect:修改成功-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_4} " \
            f"to {self.user_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.reassign_owned_success,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql('\\d')
        self.logger.info(sql_msg)
        self.assertGreater(len(sql_msg.splitlines()), 3, '执行失败' + text)
        self.assertIn(self.table_name_1, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.user_name_1, sql_msg.splitlines()[-3],
                      '执行失败' + text)

        text = "-----step6:old_role管理员用户，new_role管理员用户;expect:修改成功-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_3} " \
            f"to {self.user_name_4};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.reassign_owned_success,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql('\\d')
        self.logger.info(sql_msg)
        self.assertGreater(len(sql_msg.splitlines()), 2, '执行失败' + text)
        self.assertIn(self.table_name_2, sql_msg.splitlines()[-2],
                      '执行失败' + text)
        self.assertIn(self.user_name_4, sql_msg.splitlines()[-2],
                      '执行失败' + text)

    def tearDown(self):
        text = "-----step7:删除表，删除用户，expect:删除成功-----"
        self.logger.info(text)
        drop_cmd = f"drop table {self.table_name_1};" \
            f"drop table {self.table_name_2};" \
            f"drop user {self.user_name_1} cascade;" \
            f"drop user {self.user_name_2} cascade;" \
            f"drop user {self.user_name_3} cascade;" \
            f"drop user {self.user_name_4} cascade;"
        drop_msg = self.primary_sh.execut_db_sql(drop_cmd)
        self.logger.info(drop_msg)
        self.assertEquals(2, drop_msg.count(self.constant.TABLE_DROP_SUCCESS),
                          '执行失败' + text)
        self.assertEquals(4, drop_msg.count(
            self.constant.DROP_ROLE_SUCCESS_MSG), '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
